package com.taotao.product.service.impl;

import com.taotao.product.domain.PmsSkuImages;
import com.taotao.product.domain.PmsSkuInfo;
import com.taotao.product.domain.PmsSpuInfoDesc;
import com.taotao.product.service.*;
import com.taotao.product.vo.AttrGroupVo;
import com.taotao.product.vo.ItemInfoVo;
import com.taotao.product.vo.SaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @className: IPmsItemInfoServiceImpl
 * @author: ZhangYuQi
 * @date: 2024/12/24 下午4:05
 * @Version: 1.0
 * @description:
 */

@Service
public class IPmsItemInfoServiceImpl implements IPmsItemInfoService {

    @Autowired
    private IPmsSkuInfoService pmsSkuInfoService;

    @Autowired
    private IPmsSkuImagesService pmsSkuImagesService;

    @Autowired
    private IPmsSpuInfoDescService pmsSpuInfoDescService;

    @Autowired
    private IPmsAttrAttrgroupRelationService pmsAttrAttrgroupRelationService;

    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;

    @Override
    public ItemInfoVo getItemInfoBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        ItemInfoVo itemInfoVo = new ItemInfoVo();
        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //* 1.查sku详情   0.5s
            PmsSkuInfo pmsSkuInfo = pmsSkuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        }, itemThreadPoolExcuter);

        CompletableFuture<Void> PmsSkuImageCompletableFuture = CompletableFuture.runAsync(() -> {
            //* 2.查sku图片信息 1s
            List<PmsSkuImages> pmsSkuImages = pmsSkuImagesService.selectPmsSkuImagesBySkuId(skuId);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        }, itemThreadPoolExcuter);

        CompletableFuture<Void> SaleAttrsCompletableFuture = CompletableFuture.runAsync(() -> {
            //* 3.查spu相关信息销售属性 1s
            List<SaleAttrVo> saleAttrsBySkuId = pmsSkuInfoService.getSaleAttrsBySkuId(skuId);
            itemInfoVo.setSaleAttrs(saleAttrsBySkuId);
        }, itemThreadPoolExcuter);

        CompletableFuture<Void> PmsSpuInfoCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {
            //* 4.查spu商品介绍 1s
            PmsSpuInfoDesc pmsSpuInfoDesc = pmsSpuInfoDescService.selectPmsSpuInfoDescBySpuId(Long.valueOf(pmsSkuInfo.getSpuId()));
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        }, itemThreadPoolExcuter);

        CompletableFuture<Void> AttrGroupWithAttrCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {
            //* 5.查规格和包装 05s
            List<AttrGroupVo> attrGroupAttrs = pmsAttrAttrgroupRelationService.getAttrGroupWithAttrsByCatelogId(Long.valueOf(pmsSkuInfo.getCatalogId()));
            itemInfoVo.setAttrGroupVos(attrGroupAttrs);
        }, itemThreadPoolExcuter);

        CompletableFuture.allOf(pmsSkuInfoCompletableFuture, PmsSkuImageCompletableFuture, SaleAttrsCompletableFuture, PmsSpuInfoCompletableFuture, AttrGroupWithAttrCompletableFuture).get();


        return itemInfoVo;
    }
}
